package cn.org.timespace.easyframe.util.dictionary;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.org.timespace.easyframe.util.ds.EfDataSourceUtil;

/**
 * A data access object (DAO) providing persistence and search support for
 * EfDictionary entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see cn.org.timespace.easyframe.util.dictionary.model.EfDictionary
 * @author MyEclipse Persistence Tools
 */

public class EfDictionaryDAO {
	
	private static final Log log = LogFactory.getLog(EfDictionaryDAO.class);
	
	private static EfDictionaryDAO efDictionaryDAO = null;
	
	private EfDictionaryDAO(){}
	
	public static EfDictionaryDAO getInstance(){
		if(efDictionaryDAO == null)
			efDictionaryDAO = new EfDictionaryDAO();
		return efDictionaryDAO;
	}
	
	
	/**
	 * 获取字典表里面所有的数据
	 * @return
	 */
	public Map<String,List<EfDictionary>> getAll(){
		
		if(log.isInfoEnabled()) log.info("正在从数据库中获取数据字典项...");
		
		Map<String, List<EfDictionary>> dicMap = new HashMap<String,List<EfDictionary>>();
		String sql = "SELECT * FROM EF_DICTIONARY";
		Connection conn  = EfDataSourceUtil.getInstance().getConnection();
		
		try {
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			while(rs.next()){
				
				String groupName = rs.getString("GROUPNAME");
				
				EfDictionary dic = new EfDictionary();
				dic.setGroupname(groupName);
				dic.setType(rs.getString("TYPE"));
				dic.setItemvalue(rs.getString("ITEMVALUE"));
				dic.setItemname(rs.getString("ITEMNAME"));
				dic.setFilter(rs.getString("FILTER"));
				dic.setExesql(rs.getString("EXESQL"));
				dic.setRemark(rs.getString("REMARK"));
				
				if(dicMap.containsKey(groupName)){
					List<EfDictionary> oneList = (List<EfDictionary>) dicMap.get(groupName);
					oneList.add(dic);
				}else{
					List<EfDictionary> oneList = new ArrayList<EfDictionary>();
					oneList.add(dic);
					dicMap.put(groupName, oneList);
				}
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
			if(log.isErrorEnabled()) log.error("从数据库中获取数据字典项出错，原因是："+e.getMessage());
		}
		
		if(log.isInfoEnabled()) log.info("从数据库中获取数据字典项成功。");
		return dicMap;
	}
	
	/**
	 * 获取字典表里面所有的数据
	 * @return
	 */
	public List<EfDictionary> getDictionaryBySql(String sql,String remark){
		
		if(log.isInfoEnabled()) log.info("正在从数据库中获取数据字典项...");
		
		List<EfDictionary> dicList = new ArrayList<EfDictionary>();

		Connection conn  = EfDataSourceUtil.getInstance().getConnection();
		
		try {
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			while(rs.next()){
				
				EfDictionary dic = new EfDictionary();
				dic.setItemvalue(rs.getString("ITEMVALUE"));
				dic.setItemname(rs.getString("ITEMNAME"));
				dic.setRemark(remark);
				dicList.add(dic);
				
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
			if(log.isErrorEnabled()) log.error("从数据库中获取数据字典项出错，原因是："+e.getMessage());
		}
		
		if(log.isInfoEnabled()) log.info("从数据库中获取数据字典项成功。");
		
		return dicList;
	}
	
	/**
	 * 根据sql获取名称
	 * @return
	 */
	public String getNameBySql(String sql){
		
		if(log.isInfoEnabled()) log.info("正在从数据库中获取数据字典项...");
		
		String resName = "";
		Connection conn  = EfDataSourceUtil.getInstance().getConnection();
		
		try {
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			if(rs.next()){
				resName = rs.getString("ITEMNAME");
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
			if(log.isErrorEnabled()) log.error("从数据库中获取数据字典项出错，原因是："+e.getMessage());
		}
		
		if(log.isInfoEnabled()) log.info("从数据库中获取数据字典项成功。");
		
		return resName;
	}
	
}